package com.yanxi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yanxi.mapper.EmpExprMapper;
import com.yanxi.mapper.EmpMapper;
import com.yanxi.pojo.*;
import com.yanxi.service.EmpLogService;
import com.yanxi.service.EmpService;
import com.yanxi.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;
    @Autowired
    private EmpLogService empLogService;
    @Override
/*    public PageResult<Emp> page(Integer page, Integer pageSize) {
        long total = empMapper.count();
        Integer start = (page - 1) * pageSize;
        List<Emp> rows = empMapper.list(start, pageSize);
        return new PageResult<Emp>(total,rows);
    }*/
    public PageResult<Emp> page( EmpQueryParam empQueryParam) {
        PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
        List<Emp> empList = empMapper.list(empQueryParam);
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<Emp>(p.getTotal(), p.getResult());
    }

    @Transactional
    @Override
    public void save(Emp emp) {
        try {
            //1.保存员工信息
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.save(emp);
            // 2.保存员工工作经历信息
            List<EmpExpr> exprList = emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                //为每个员工工作经历设置员工id
                exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
                empExprMapper.insertBatch(exprList);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 3.记录日志
            EmpLog empLog = new EmpLog(null,LocalDateTime.now(),"添加员工："+emp);
            empLogService.insertLog(empLog);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Integer> ids) {
        //1.删除员工信息
        empMapper.deleteByIds(ids);
        //2.删除员工工作经历信息
        empExprMapper.deleteByEmpIds(ids);
    }

    //3.查询回显&修改员工信息
    @Override
    public Emp get(Integer id) {
        //分开查
        /*//1.查询员工信息
        Emp emp = empMapper.get(id);
        if(emp != null){
            //2.查询员工工作经历信息
            List<EmpExpr> exprList = empExprMapper.listByEmpId(id);
            emp.setExprList(exprList);
            return emp;
        }
        return null;*/
        return empMapper.get(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //1.修改员工信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
        //2.修改员工工作经历信息
        //2.1  删除员工工作经历信息
        empExprMapper.deleteByEmpIds(List.of(emp.getId()));
        //2.2  添加员工工作经历信息
        if(!CollectionUtils.isEmpty(emp.getExprList())){
           emp.getExprList().forEach(empExpr -> empExpr.setEmpId(emp.getId()));
           empExprMapper.insertBatch(emp.getExprList());
        }

    }

    @Override
    public List<Emp> listAll() {
        return empMapper.listAll();

    }


    //登录
    @Override
    public LoginInfo login(Emp emp) {
        //1.查询员工信息通过password, username
        Emp empDB = empMapper.selectByUsernameAndPassword(emp);
        //2.判断是否存在员工
        if(empDB != null){
            //生成令牌
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",empDB.getId());
            claims.put("username",empDB.getUsername());
            String token = JwtUtils.generateToken(claims);
            return new LoginInfo(empDB.getId(),empDB.getUsername(),empDB.getName(),token);
        }else{
            return null;
        }
    }
}
